package com.hzy.login;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hzy.text.R;

/**
 * Created by Administrator on 2016/6/23 0023.
 */
public class RegisterActivity extends AppCompatActivity {
    private EditText registerEditTV_1;
    private EditText registerEditTV_2;
    private Button registerBtn;
    private Button returnBtn;
    private String registerUserName;
    private String registerUserCode;
    private UserDBHelper userDBHelper;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        userDBHelper =new UserDBHelper(this,"User.db",null,1);
        findView();
        initView();
    }

    private  void findView(){
        registerEditTV_1 = (EditText) findViewById(R.id.register_edit_1);
        registerEditTV_2 = (EditText) findViewById(R.id.register_edit_2);
        registerBtn = (Button) findViewById(R.id.register_btn);
        returnBtn = (Button) findViewById(R.id.return_btn);
    }

    private void initView(){

        registerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                registerUserName = registerEditTV_1.getText().toString();
                registerUserCode = registerEditTV_2.getText().toString();

                SQLiteDatabase db = userDBHelper.getWritableDatabase();
                Cursor cursor = db.rawQuery("select * from User where userName = ?",new String[]{registerUserName});
                if (cursor.moveToFirst()){
                    Toast.makeText(RegisterActivity.this,"该账号已存在，请重新注册",Toast.LENGTH_SHORT).show();
                } else {
                    ContentValues values = new ContentValues();
                    values.put("userName",registerUserName);
                    values.put("userCode",registerUserCode);
                    db.insert("User",null,values);
                    values.clear();

                    Toast.makeText(RegisterActivity.this,"注册成功，请登录",Toast.LENGTH_SHORT).show();
                }
            }
        });

        returnBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
}
